<template>
  <view class="page-container">
    <view class="list">
      <view class="list-item" @tap="handlerVersion">
        <text class="label">版本号</text>
        <text class="value">{{version}}</text>
      </view>
      <view class="list-item" @tap="handlerCache">
        <text class="label">清除缓存</text>
        <text class="value">{{cache}}</text>
      </view>
    </view>
    <button class="logout" type="primary" @tap="handlerLogout()">退出</button>

    <!-- 弹出层 -->
    <uni-popup ref="popupVersion" type="center">当前已是最新版本！</uni-popup>
    <uni-popup ref="popupCache" type="center">清除缓存成功！</uni-popup>
  </view>
</template>

<script>
  import uniPopup from '@/components/uni-popup/uni-popup.vue'
  export default {
    components: {
      uniPopup
    },
    data() {
      return {
        isLogin: false,
        version: '1.2.3',
        cache: '0'
      }
    },
    onShow() {
      let that = this;
	  // #ifdef APP-PLUS
      plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
        that.version = wgtinfo.version
      });
	  // #endif
      let auth_token = uni.getStorageSync("auth_token");
      if (auth_token) {
        this.isLogin = true
      } else {
        this.isLogin = false
      }
      // 缓存
      // #ifdef APP-PLUS
      this.calcCache()
      // #endif
    },
    methods: {
      /**  
       * 计算缓存大小  官方提供方法，用于iOS  
       */
      calcCache() {
        console.log("开始计算缓存大小");
        let finalSize = -1;
        let that = this;
        plus.cache.calculate(function(size) {
          let sizeCache = parseInt(size);
          if (sizeCache == 0) {
            finalSize = "0B";
          } else if (sizeCache < 1024) {
            finalSize = sizeCache + "B";
          } else if (sizeCache < 1048576) {
            finalSize = (sizeCache / 1024).toFixed(2) + "KB";
          } else if (sizeCache < 1073741824) {
            finalSize = (sizeCache / 1048576).toFixed(2) + "MB";
          } else {
            finalSize = (sizeCache / 1073741824).toFixed(2) + "GB";
          }
          that.cache = finalSize
          // return callback(finalSize);
        });
      },
      // 检查版本
      handlerVersion() {
        this.$refs.popupVersion.open()
      },
      // 清空缓存
      handlerCache() {
        // clearCache() {
        let that = this;
        let os = plus.os.name;
        if (os == 'Android') {
          let main = plus.android.runtimeMainActivity();
          let sdRoot = main.getCacheDir();
          let files = plus.android.invoke(sdRoot, "listFiles");
          let len = files.length;
          for (let i = 0; i < len; i++) {
            let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
            plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
              if (entry.isDirectory) {
                entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
                  // uni.showToast({
                  //   title: '缓存清理完成',
                  //   duration: 2000
                  // });
                  that.$refs.popupCache.open()
                  that.calcCache(); // 重新计算缓存  
                }, function(e) {
                  console.log(e.message)
                });
              } else {
                entry.remove();
              }
            }, function(e) {
              console.log('文件路径读取失败')
            });
          }
        } else { // ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错  
          plus.cache.clear(function() {
            // uni.showToast({
            //   title: '缓存清理完成',
            //   duration: 2000
            // });
            that.$refs.popupCache.open()
            that.calcCache();
          });
        }
      },
      // 退出登录
      handlerLogout() {
        if (!this.isLogin) {
          return
        }
        uni.showModal({
          title: '提示',
          content: "确定退出登录吗?",
          confirmText: '确定',
          cancelText: '取消',
          confirmColor: '#000000',
          cancelColor: '#999999',
          success: (res) => {
            if (res.confirm) {
              uni.showToast({
                icon: 'none',
                title: '退出成功',
                duration: 1000
              });

              setTimeout(() => {
                uni.removeStorageSync('auth_token');
                uni.removeStorageSync('user_id');
				uni.removeStorageSync('userInfo');
                uni.redirectTo({
                  url: '/pages/login/login'
                })
              }, 1000)
            }
          }
        })
      }
    }
  }
</script>

<style>
  page {
    height: 100%;
    border-top: 2upx solid #E3E3E3;
  }

  .page-container {
    height: 100%;
    position: relative;
  }

  .list-item {
    height: 90upx;
    padding-left: 30upx;
    padding-right: 30upx;
    line-height: 90upx;
    border-bottom: 2upx solid #F4F4F4;
    font-size: 30upx;
    font-weight: 400;
    display: flex;
    justify-content: space-between;
  }

  .list-item .label {
    color: #999;
  }

  .list-item .value {
    color: #333;
  }

  .logout {
    position: absolute;
    left: 0;
    bottom: 0;
    bottom: env(safe-area-inset-bottom);
    bottom: const(safe-area-inset-bottom);
    width: 100%;
  }

  .logout[type=primary] {
    line-height: 100upx;
    border-radius: 0;
    background: #AD85FC;
    font-size: 34upx;
  }

  .logout[disabled][type=primary] {
    background-color: #F4F4F4;
    color: #666;
  }

  .logout:after {
    display: none;
  }
</style>
